package pri.zjy.interview;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangjy
 * @description
 * @date 2025/6/13 16:27
 */
public class Test9 {
    // 多线程打印A、B、C，每个线程分别打印一个字母，按照A、B、C顺序；可打印多次

    private static volatile boolean flagA = true;

    private static volatile boolean flagB = false;

    private static volatile boolean flagC = false;

    private static AtomicInteger atomicInteger = new AtomicInteger(6);

    public static void main(String[] args) {

//        print();

        Thread threadA = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                while (!flagA) {

                }
                System.out.println("A");
                flagA = false;
                flagB = true;
            }

        });
        Thread threadB = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                while (!flagB) {

                }
                System.out.println("B");
                flagB = false;
                flagC = true;
            }

        });
        Thread threadC = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                while (!flagC) {

                }
                System.out.println("C");
                flagC = false;
                flagA = true;
            }

        });
        threadA.start();
        threadB.start();
        threadC.start();
    }

    public static void print() {
        Thread threadA = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                if (flagA) {
                    System.out.println("A");
                    flagA = false;
                    flagB = true;
                }
            }

        });
        Thread threadB = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                if (flagB) {
                    System.out.println("B");
                    flagB = false;
                    flagC = true;
                }
            }

        });
        Thread threadC = new Thread(() -> {
            for (int i = 0; i < 3; i++) {
                if (flagC) {
                    System.out.println("C");
                    flagC = false;
                    flagA = true;
                }
            }

        });
        threadA.start();
        threadB.start();
        threadC.start();
    }

}